package inter;

import fn.Add;
import fn.Constant;
import fn.Cosine;
import fn.Divide;
import fn.Function;
import fn.Latex;
import fn.Multiply;
import fn.Subtract;
import fn.Variable;

public class Lagrange extends Interpolation {

	public Lagrange() {}

	public Function solve(double[][] xy) {
		int n = xy.length;
		Function add = Constant.ZERO;
		for (int j = 0; j < n; j++) {
			Function multiply = Constant.create(xy[j][1]);
			for (int k = 0; k < n; k++)
				if (k != j)
					multiply = Multiply.create(
							multiply,
							Divide.create(Subtract.create(Variable.create(), Constant.create(xy[k][0])),
									Subtract.create(Constant.create(xy[j][0]), Constant.create(xy[k][0]))));
			add = Add.create(add, multiply);

		}
		return add;
	}

	@Override
	public String toString() {
		return "Interpolation";
	}

	public static void main(String[] args) {
		double xy[][] = new double[2][2];
		xy[1][0] = 1;
		xy[1][1] = 0.362358;
		xy[0][0] = 0.0;
		xy[0][1] = 1.2;
		Interpolation pl = new Lagrange();
		Function fn = pl.getError(xy, Cosine.create(Variable.create()));
		Latex.show(fn.toLatex());
	}
}